/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.constant.CacheConstants;
import com.excesys.exsecs.common.core.constant.enums.DictTypeEnum;
import com.excesys.exsecs.common.core.exception.ErrorCodes;
import com.excesys.exsecs.common.core.util.MsgUtils;
import com.excesys.exsecs.om.entity.OmODict;
import com.excesys.exsecs.om.entity.OmODictItem;
import com.excesys.exsecs.om.mapper.OmODictItemMapper;
import com.excesys.exsecs.om.mapper.OmODictMapper;
import com.excesys.exsecs.om.service.OmODictItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 字典项
 *
 * @author weiyj
 * @date 2023-02-24 13:53:35
 */
@Service
@RequiredArgsConstructor
public class OmODictItemServiceImpl extends ServiceImpl<OmODictItemMapper, OmODictItem> implements OmODictItemService {

	private final OmODictMapper omODictMapper;

	/**
	 * 删除字典项
	 * @param id 字典项ID
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public void removeDictItem(Long id) {
		// 根据ID查询字典ID
		OmODictItem dictItem = this.getById(id);
		OmODict dict = omODictMapper.selectById(dictItem.getDictId());
		// 系统内置
		Assert.state(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystemFlag()),
				MsgUtils.getMessage(ErrorCodes.SYS_DICT_DELETE_SYSTEM));
		this.removeById(id);
	}

	/**
	 * 更新字典项
	 * @param item 字典项
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, key = "#item.type")
	public void updateDictItem(OmODictItem item) {
		// 查询字典
		OmODict dict = omODictMapper.selectById(item.getDictId());
		// 系统内置
		Assert.state(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystemFlag()),
				MsgUtils.getMessage(ErrorCodes.SYS_DICT_UPDATE_SYSTEM));
		this.updateById(item);
	}

}
